package com.zzh.net.net;


import com.zzh.net.BuildConfig;
import com.zzh.net.Net;
import com.zzh.net.interceptor.HeaderInterceptor;
import com.zzh.net.interceptor.HttpErrorRecordInterceptor;
import com.zzh.net.interceptor.LogInterceptor;
import com.zzh.net.interceptor.UrlInterceptor;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * @author zhangzhihao
 * @email 1016234015@qq.com
 * @date 2020-08-29
 * @desc NetRetrofitManager 网络请求封装
 */

public class NetRetrofitManager {
    protected static NetRetrofitManager retrofitManager;
    protected Retrofit retrofit;
    protected OkHttpClient okHttpClient = null;

    /**
     * 连接超时时间5秒
     */
    protected static final int RETROFIT_TIMEOUT = 15;

    protected static int timeOut = RETROFIT_TIMEOUT;
    /**
     * 写入超时时间15秒
     */
    protected static final int RETROFIT_WRITE_TIMEOUT = 15;

    /**
     * 单例模式
     * String baseUrl
     *
     * @return
     */
    public static NetRetrofitManager getInstance() {
        if (retrofitManager == null) {
            synchronized (NetRetrofitManager.class) {
                if (retrofitManager == null) {
                    retrofitManager = new NetRetrofitManager();
                }
            }
        }
        return retrofitManager;
    }

    private NetRetrofitManager() {
        initRetrofitManager();
    }

    /**
     * json 类型解析
     */
    private void initRetrofitManager() {
        //创建Retrofit实例
        retrofit = new Retrofit.Builder()
                .baseUrl(Net.getInstance().getBaseUrls().get(0))
                .client(getOkHttpClient())
                // 支持 String 类型解析
                .addConverterFactory(ScalarsConverterFactory.create())
                // 支持 Gson 类型解析
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    /**
     * 日志开始
     * 获取okHttp
     */
    private OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            if (BuildConfig.DEBUG) {
                // 日志拦截器
                builder.addInterceptor(new LogInterceptor());
            }
            // header拦截器
            builder.addInterceptor(new HeaderInterceptor())
                    .addInterceptor(new UrlInterceptor())
                    .addInterceptor(new HttpErrorRecordInterceptor())
                    // 重试拦截器
//                    .addInterceptor(new RetryIntercepter(1))
                    // 设置当网络有问题时进行尝试
                    .retryOnConnectionFailure(true)
                    //设置连接超时时间
                    .connectTimeout(timeOut, TimeUnit.SECONDS)
                    //设置读取超时时间
                    .readTimeout(timeOut, TimeUnit.SECONDS)
                    //设置写入超时时间
                    .writeTimeout(timeOut, TimeUnit.SECONDS)
                    .build();
            okHttpClient = builder.build();
        }
        return okHttpClient;
    }


    /**
     * 网络请求的反射用集合
     */
    private static final Map<String, NetServer> netServer = new HashMap<>();

    /**
     * 创建相应的服务接口
     */
    public <T extends NetServer> T newServer(Class<T> reqServer) {
        String name = reqServer.getName();
        if (netServer.containsKey(name)) {
            return (T) netServer.get(name);
        }
        T t = retrofit.create(reqServer);
        netServer.put(name, t);
        return t;
    }
}
